package com.javaProject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javaProject.dto.R;
import com.javaProject.entity.Student;
import com.javaProject.entity.Teacher;
import com.javaProject.mapper.TeacherMapper;
import com.javaProject.service.StudentService;
import com.javaProject.mapper.StudentMapper;
import com.javaProject.service.UserkeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.InvalidAlgorithmParameterException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;
import java.util.Objects;

/**
* @author ASUS
* @description 针对表【student】的数据库操作Service实现
* @createDate 2024-03-15 11:50:54
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService{

    @Autowired
    SM2 sm2;

    @Autowired
    UserkeyService keyService;

    @Autowired
    TeacherMapper teacherMapper;

    @Override
    public R login(Student student) throws NoSuchAlgorithmException, InvalidKeySpecException {
        QueryWrapper<Student> query = Wrappers.query(Student.class);
        query.eq("username",student.getUsername());
        Student one = getOne(query);
        if (Objects.isNull(one)){
            return R.fail().setMsg("该用户不存在").setCode(-2);
        }
        String password = sm2.decrypt(one.getPassword(),keyService.getPrivateKey(student.getUsername()));
        System.out.println(password);
        one.setPassword(password);
        if (Objects.equals(student.getPassword(),password)){
            return R.ok().setData(one);
        }
        return R.fail().setMsg("密码错误").setCode(-1);
    }

    @Override
    public R add(Student student) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
        QueryWrapper<Student> query = Wrappers.query(Student.class);
        query.eq("username",student.getUsername());
        KeyPair keyPair = sm2.generateKeyPair();
        System.out.println(keyPair.getPublic());
        String pasword = sm2.encrypt(student.getPassword(),keyPair.getPublic());
        keyService.saveKey(student.getUsername(),keyPair.getPrivate());
        student.setPassword(pasword);
        Student one = getOne(query);
        if (Objects.nonNull(one)){
            return R.fail().setMsg("该用户已存在").setCode(-1);
        }
        Teacher teachers = teacherMapper.selectOne(Wrappers.<Teacher>query().eq("username",student.getUsername()));
        if (Objects.nonNull(teachers)){
            return R.fail().setMsg("该用户已存在").setCode(-1);
        }
        if (save(student)){
            return R.ok().setMsg("用户注册成功").setData(student);
        }
        return R.fail().setMsg("注册失败");
    }

    @Override
    public R Delect(Student student) {
        QueryWrapper<Student> query = Wrappers.query(Student.class);
        query.eq("username",student.getUsername());
        Student one = getOne(query);
        if (Objects.isNull(one)){
            return R.fail().setMsg("用户不存在").setCode(-2);
        }
        if (remove(query)){
            return R.ok().setMsg("用户删除成功");
        }
        return R.fail().setMsg("用户信息删除失败");
    }

    @Override
    public R upData(Student student) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
        QueryWrapper<Student> query = Wrappers.query(Student.class);
        query.eq("username",student.getUsername());
        Student one = getOne(query);
        if (Objects.isNull(one)){
            return R.fail().setMsg("用户不存在").setCode(-2);
        }
        KeyPair keyPair = sm2.generateKeyPair();
        System.out.println(keyPair.getPublic());
        String pasword = sm2.encrypt(student.getPassword(),keyPair.getPublic());
        keyService.saveKey(student.getUsername(),keyPair.getPrivate());
        student.setPassword(pasword);
        boolean updateResult = update(student, query);

        if (updateResult) {
            return R.ok().setMsg("用户信息修改成功").setData(student);
        }
        return R.fail().setMsg("用户信息修改失败");
    }

    @Override
    public List<Student> getAllTeacher() throws NoSuchAlgorithmException, InvalidKeySpecException {
        QueryWrapper<Student > query = Wrappers.query(Student.class);
        List<Student>  students= list(query);
        for (Student student : students) {
            System.out.println(student.getUsername());
            System.out.println(student.getPassword());
            String decryptedPassword = sm2.decrypt(student.getPassword(),keyService.getPrivateKey(student.getUsername())); // 解密密码
            student.setPassword(decryptedPassword); // 设置明文密码
        }
        return  students;
    }

    @Override
    public Student getOneStudent(int student_id) throws NoSuchAlgorithmException, InvalidKeySpecException {
        QueryWrapper<Student> query = Wrappers.query(Student.class);
        query.eq("student_id",student_id);
        Student one = getOne(query);
        String password = sm2.decrypt(one.getPassword(),keyService.getPrivateKey(one.getUsername()));
        one.setPassword(password);
        return one;
    }


}




